<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="doAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerRole(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="doEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="doDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="roleQuery.page"
                :page-sizes="[3, 6, 9, 12]"
                :page-size="roleQuery.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 编辑添加对话框 -->
      <el-dialog :title="isEdit?'编辑角色':'添加角色'" :visible.sync="showDialog" @close="resetForm">
        <el-form ref="formRef" :model="formRole" label-width="100px" :rules="formRules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formRole.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formRole.description" />
          </el-form-item>
          <el-button size="small" style="margin-left:50%" @click="showDialog=false">取消</el-button>
          <el-button size="small" type="success" @click="hSuccess">确认</el-button>
        </el-form>
      </el-dialog>

      <!-- 权限配置对话框 -->
      <el-dialog title="权限设置" :visible.sync="settingRoleDialog">
        <assignPermission @close="settingRoleDialog=false" v-if="settingRoleDialog" :id="cruId" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, addRole, updateRole, delRole } from '@/api/settings'
import assignPermission from './assignPermission'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      cruId: '',
      settingRoleDialog: false,
      isEdit: false,
      showDialog: false,
      total: 0,
      list: [], // 所有角色列表
      roleQuery: {
        page: 1,
        pagesize: 3
      },
      formRole: {
        name: '',
        description: ''
      },
      formRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadRoleList()
  },
  methods: {
    // 传id给子组件 开启对话框
    assignPerRole(id) {
      this.cruId = id
      this.settingRoleDialog = true
    },
    // 关闭dialog 重置表单
    resetForm() {
      this.$refs.formRef.resetFields()
      this.formRole = {
        name: '',
        description: ''
      }
    },
    // 删除角色
    doDel(id) {
      this.$confirm('确定删除此角色?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRole(id)
        if (this.list.length === 1) this.roleQuery.page--
        console.log(this.roleQuery.page)
        this.loadRoleList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 更新角色信息
    async hEdit() {
      try {
        await updateRole(this.formRole)
        this.showDialog = false
        this.loadRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑角色渲染内容
    doEdit(data) {
      // console.log(data)
      this.showDialog = true
      this.isEdit = true
      this.formRole = { ...data }
    },
    // 添加角色
    async hAdd() {
      try {
        await addRole(this.formRole)
        this.showDialog = false
        this.total++
        this.roleQuery.page = Math.ceil(this.total / this.roleQuery.pagesize)
        this.loadRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    // 打开对话框 新增
    doAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    // 复用弹框 确认按钮
    hSuccess() {
      this.$refs.formRef.validate(valid => {
        if (!valid) return
        this.isEdit ? this.hEdit() : this.hAdd()
      })
    },
    // 获取所有角色列表
    async loadRoleList() {
      try {
        const res = await getRoleList(this.roleQuery)
        this.list = res.data.rows
        // console.log(res.data.rows)
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    // 分页器
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.roleQuery.page = 1
      this.roleQuery.pagesize = val
      this.loadRoleList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.roleQuery.page = val
      this.loadRoleList()
    }
  }
}
</script>
